package com.zzq.service;


import com.zzq.tools.MinioTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class MinioStorageService {

    @Autowired
    private MinioTemplate minioTemplate;

    @Value("${minio.bucket}")
    private String bucketName;

    /**
     * 获取文件外链
     *
     * @param objectName 文件名称
     */
    public String getObjectURL(String objectName) {
        try {
            return minioTemplate.getObjectURL(bucketName, objectName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String uploadFile(InputStream inputStream, String filePath) {
        String path = null;
        try {
            minioTemplate.putObject(bucketName, filePath, inputStream);
            path = filePath;
        } catch (Exception e) {

        }
        return path;
    }

    public InputStream downloadFile(String filePath) {
        InputStream inputStream = null;
        try {
            inputStream = minioTemplate.getObject(bucketName, filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    public void removeFile(String filePath){
        try{
            minioTemplate.removeObject(bucketName,filePath);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void createBucket(String bucketName) {
        try {
            minioTemplate.createBucket(bucketName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
